package com.hgyc.mom.wam.controller;

import java.time.LocalDateTime;
import java.util.*;

import cn.dev33.satoken.stp.StpUtil;
import com.hgyc.mom.common.enums.CommonOrderStatusEnum;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamCheckReport;
import com.hgyc.mom.eam.vo.EamCheckReportParam;
import com.hgyc.mom.eam.service.EamCheckReportService;
import com.hgyc.mom.eam.vo.EamCheckReportVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备点检Controller
 *
 * @author fwj
 * @since 2025-08-14
 */
@Tag(name = "设备点检控制器", description = "设备点检控制器")
@RestController
@RequestMapping("/eam/check/report")
public class EamCheckReportController {
    @Autowired
    private EamCheckReportService eamCheckReportService;

    @Operation(summary = "查询设备点检列表", description = "查询设备点检列表")
    @PostMapping("/pageList")
    public Result<IPage<EamCheckReport>> pageList(@Parameter(description = "查询参数") @RequestBody EamCheckReportParam eamCheckReportParam) {
        Page<EamCheckReport> page = new Page<>(eamCheckReportParam.getPage(), eamCheckReportParam.getPageSize());
        EamCheckReport eamCheckReport = new EamCheckReport();
        BeanUtils.copyProperties(eamCheckReportParam, eamCheckReport);
        QueryWrapper<EamCheckReport> wrapper = QueryWrapperUtil.build(eamCheckReport);
        IPage<EamCheckReport> list = eamCheckReportService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取设备点检详细信息", description = "获取设备点检详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamCheckReport> getInfo(@Parameter(description = "设备点检ID") @PathVariable("id") Long id) {
        return Result.success(eamCheckReportService.getById(id));
    }
    @Operation(summary = "新增设备点检", description = "新增设备点检")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备点检") @RequestBody EamCheckReportVO eamCheckReportVO) {
        eamCheckReportService.saveCheckReport(eamCheckReportVO);
        return Result.success();
    }

    @Operation(summary = "修改设备点检", description = "修改设备点检")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备点检") @RequestBody EamCheckReportVO eamCheckReportVO) {
        eamCheckReportService.updateCheckReport(eamCheckReportVO);
        return Result.success();
    }

    @Operation(summary = "提交设备点检记录", description = "提交设备点检记录")
    @PostMapping("/submit")
    public Result<?> submit(@Parameter(description = "设备点检") @RequestBody EamCheckReportVO eamCheckReportVO) {
        eamCheckReportVO.setStatus(CommonOrderStatusEnum.APPROVING.getValue());
        eamCheckReportVO.setCheckBy(StpUtil.getLoginIdAsLong());
        eamCheckReportVO.setCheckTime(LocalDateTime.now());
        eamCheckReportService.updateCheckReport(eamCheckReportVO);
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PutMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable("status") String status, @RequestParam("ids") Long[] ids) {
        eamCheckReportService.updateStatus(status, Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "删除设备点检", description = "删除设备点检")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        eamCheckReportService.removeCheckReport(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamCheckReport> util = new ExcelUtil<>(EamCheckReport.class);
        util.importTemplateExcel(response, "设备点检");
    }

    @Operation(summary = "导出设备点检列表", description = "导出设备点检列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamCheckReportParam eamCheckReportParam) {
        EamCheckReport eamCheckReport = new EamCheckReport();
        BeanUtils.copyProperties(eamCheckReportParam, eamCheckReport);
        QueryWrapper<EamCheckReport> wrapper = QueryWrapperUtil.build(eamCheckReport);
        List<EamCheckReport> list = eamCheckReportService.list(wrapper);
        ExcelUtil<EamCheckReport> util = new ExcelUtil<>(EamCheckReport.class);
        util.exportExcel(response, list, "设备点检数据");
    }

    @Operation(summary = "导入设备点检列表", description = "导入设备点检列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamCheckReport> util = new ExcelUtil<>(EamCheckReport.class);
        List<EamCheckReport> eamCheckReportList = util.importExcel(file.getInputStream());
        String message = eamCheckReportService.importData(eamCheckReportList, updateSupport);
        return Result.success(message);
    }
}
